import Toast from "@vant/weapp/toast/toast";

Page({
  /**
   * 登录
   */
  onLogin1() {
    //  1. 获取用户加密数据
    wx.getUserProfile({
      desc: "desc",
      success: (res) => {
        console.log("=== getUserProfile", res);
        // 取出 encryptedData 与 iv
        const { encryptedData, iv } = res;

        //  2. 获取 code
        wx.login({
          success: (res) => {
            console.log("=== login code", res.code);
            // 取出 code
            const { code } = res;

            // 3. 调用后端小程序登录接口
            wx.request({
              method: "POST",
              url: "http://127.0.0.1:8001/app/user/login/mini",
              data: {
                code,
                encryptedData,
                iv,
              },
              success: ({ data }) => {
                if (data.code !== 1000) {
                  wx.showToast({ title: data.message, icon: "none" });
                  return;
                }

                // wx.showToast({ title: "登录成功" });
                Toast({
                  message: "登录成功",
                  onClose: () => {
                    // 4. 存储 token
                    wx.setStorageSync("token", data.data.token);

                    // 5. 获取数据通道对象
                    const eventChannel = this.getOpenerEventChannel();
                    // 6. 触发 loginSuccess 事件
                    eventChannel.emit("loginSuccess");
                    // 7. 跳转 页面
                    wx.navigateBack();
                  },
                });
              },
            });
          },
        });
      },
      fail: (error) => {
        console.log("=== getUserProfile fail", error);
      },
    });
  },

  pmLogin() {
    return new Promise((resolve) => {
      wx.login({
        success:(res)=>{
          resolve(res.code)
        }
      })
    })
  },

  async onLogin2() {
    //1.获取用户加密数据
    const {encryptedData,iv} = await wx.getUserProfile({
      desc: 'desc',
    })
    // console.log('=== userInfo',encryptedData,iv);
    const code = await this.pmLogin()

            wx.login({
          success: (res) => {
            console.log("=== login code", res.code);
            // 取出 code
            const { code } = res;

            // 3. 调用后端小程序登录接口
            wx.request({
              method: "POST",
              url: "http://127.0.0.1:8001/app/user/login/mini",
              data: {
                code,
                encryptedData,
                iv,
              },
              success: ({ data }) => {
                if (data.code !== 1000) {
                  wx.showToast({ title: data.message, icon: "none" });
                  return;
                }

                // wx.showToast({ title: "登录成功" });
                Toast({
                  message: "登录成功",
                  onClose: () => {
                    // 4. 存储 token
                    wx.setStorageSync("token", data.data.token);

                    // 5. 获取数据通道对象
                    const eventChannel = this.getOpenerEventChannel();
                    // 6. 触发 loginSuccess 事件
                    eventChannel.emit("loginSuccess");
                    // 7. 跳转 页面
                    wx.navigateBack();
                  },
                });
              },
            });
          },
        });
  } ,

  async onPhone(event){
    console.log('onPhone',event);

    const {encryptedData,iv} = event.detail

    const code = await this.pmLogin()

    wx.login({
      success: (res) => {
        console.log("=== login code", res.code);
        // 取出 code
        const { code } = res;

        // 3. 调用后端小程序登录接口
        wx.request({
          method: "POST",
          url: "http://127.0.0.1:8001/app/user/login/mini",
          data: {
            code,
            encryptedData,
            iv,
          },
          success: ({ data }) => {
            console.log('data手机号',data);
            if (data.code !== 1000) {
              wx.showToast({ title: data.message, icon: "none" });
              return;
            }

            // wx.showToast({ title: "登录成功" });
          },
        });
      },
    });
  }
});